package com.bw.kanjia.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.bw.common.utils.R;
import com.bw.kanjia.dao.KanjiaActivityDao;
import com.bw.kanjia.entity.KanjiaActivityEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.common.utils.PageUtils;
import com.bw.common.utils.Query;

import com.bw.kanjia.dao.KanjiaItemDao;
import com.bw.kanjia.entity.KanjiaItemEntity;
import com.bw.kanjia.service.KanjiaItemService;


@Service("kanjiaItemService")
public class KanjiaItemServiceImpl extends ServiceImpl<KanjiaItemDao, KanjiaItemEntity> implements KanjiaItemService {

    @Autowired
    private KanjiaActivityDao kanjiaActivityDao;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private KanjiaItemDao kanjiaItemDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<KanjiaItemEntity> page = this.page(
                new Query<KanjiaItemEntity>().getPage(params),
                new QueryWrapper<KanjiaItemEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R add(Integer userid, String id) {
        //根据key取值
        String s = stringRedisTemplate.opsForValue().get(id);
        KanjiaActivityEntity kanjiaActivityEntity = JSON.parseObject(s, new TypeReference<KanjiaActivityEntity>() {
        });
        KanjiaItemEntity kanjiaItemEntity = new KanjiaItemEntity();
        kanjiaItemEntity.setGoodPrice(kanjiaActivityEntity.getGoodsPrice());
        kanjiaItemEntity.setGoodsId(kanjiaActivityEntity.getGoodsId());
        kanjiaItemEntity.setBkhdId(userid);
        kanjiaItemEntity.setGoodsUserStatic(0);
        kanjiaItemEntity.setXgTime(new Date());
        kanjiaItemDao.insert(kanjiaItemEntity);
//        if(kanjiaActivityEntity.getKanRule()==1) {
//            Random random = new Random();
//            double v = random.nextDouble()*100;
//            BigDecimal bigDecimal = new BigDecimal(v);
//            kanjiaItemEntity.set
//        }else {
//            BigDecimal bigDecimal = new BigDecimal(50);
//            kanjiaItemEntity.setGoodPrice();
//        }
        return R.ok();
    }

//    public static void main(String[] args) {
//        double v = new Random().nextDouble()*100;
//        BigDecimal bigDecimal = new BigDecimal(v);
//
//        System.out.println(bigDecimal.intValue());
//    }
}